<?php

namespace app\admin\controller\point;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * Class Pointgoods
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="积分商品管理")
 */
class Pointgoods extends AdminController
{
    use Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\common\model\PointGoods();

    }

    public function getPointByCate()
    {
        $data = [
            [
                'id' => 2,
                'title' => "快递次数兑换"
            ]
        ];
        return Show::json_show(Status::mini_normal,'ok',$data);
    }

    /**
     * @NodeAnotation (title="编辑")
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $row['description'] = htmlspecialchars_decode($row['description']);
        $this->assign('row', $row);
        return $this->fetch();
    }

}